package com.beerandjesus.sportofkings.handicapper.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Date: Jun 8, 2012
 * Time: 1:11:32 PM
 */
public class ConnectionManagerImpl implements ConnectionManager {

    private String turfSireDatabaseUsername;
    private String turfSireDatabaseUrl;
    private String turfSireDatabasePassword;
    private String turfSireDatabaseDriver;
    private String jockeyDatabaseDriver;
    private String jockeyDatabaseUsername;
    private String jockeyDatabaseUrl;
    private String jockeyDatabasePassword;


    public Connection getTurfSireDatabaseConnection() {
        try {
            return DriverManager.getConnection(getTurfSireDatabaseUrl(), getTurfSireDatabaseUsername(),
                    getTurfSireDatabasePassword());
        } catch (SQLException e) {
            throw new DatabaseException("Error getting connection to the turf sire database", e);
        }
    }

    public void releaseConnection(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                // Ignore this for now
            }
        }
    }

    public void init() {
        try {
            Class.forName(getTurfSireDatabaseDriver());
            Class.forName(getJockeyDatabaseDriver());
        } catch (ClassNotFoundException e) {
            throw new DatabaseException("Could not register turf sire driver", e);
        }

    }

    public String getTurfSireDatabaseDriver() {
        return turfSireDatabaseDriver;
    }

    public Connection getJockeyDatabaseConnection() {
        try {
            return DriverManager.getConnection(getJockeyDatabaseUrl(), getJockeyDatabaseUsername(),
                    getJockeyDatabasePassword());
        } catch (SQLException e) {
            throw new DatabaseException("Error getting connection to the jockey database", e);
        }
    }

    public String getJockeyDatabaseDriver() {
        return jockeyDatabaseDriver;
    }

    public void setTurfSireDatabaseDriver(String turfSireDatabaseDriver) {
        this.turfSireDatabaseDriver = turfSireDatabaseDriver;
    }

    public void setJockeyDatabaseDriver(String jockeyDatabaseDriver) {
        this.jockeyDatabaseDriver = jockeyDatabaseDriver;
    }

    public String getJockeyDatabaseUsername() {
        return jockeyDatabaseUsername;
    }

    public void setJockeyDatabaseUsername(String jockeyDatabaseUsername) {
        this.jockeyDatabaseUsername = jockeyDatabaseUsername;
    }

    public String getJockeyDatabaseUrl() {
        return jockeyDatabaseUrl;
    }

    public void setJockeyDatabaseUrl(String jockeyDatabaseUrl) {
        this.jockeyDatabaseUrl = jockeyDatabaseUrl;
    }

    public String getJockeyDatabasePassword() {
        return jockeyDatabasePassword;
    }

    public void setJockeyDatabasePassword(String jockeyDatabasePassword) {
        this.jockeyDatabasePassword = jockeyDatabasePassword;
    }

    public String getTurfSireDatabaseUrl() {
        return turfSireDatabaseUrl;
    }

    public void setTurfSireDatabaseUrl(String turfSireDatabaseUrl) {
        this.turfSireDatabaseUrl = turfSireDatabaseUrl;
    }

    public String getTurfSireDatabaseUsername() {
        return turfSireDatabaseUsername;
    }

    public void setTurfSireDatabaseUsername(String turfSireDatabaseUsername) {
        this.turfSireDatabaseUsername = turfSireDatabaseUsername;
    }

    public String getTurfSireDatabasePassword() {
        return turfSireDatabasePassword;
    }

    public void setTurfSireDatabasePassword(String turfSireDatabasePassword) {
        this.turfSireDatabasePassword = turfSireDatabasePassword;
    }
}
